package com.crayon.scoreboard.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.crayon.scoreboard.item.ItemGame;
import com.crayon.scoreboard.item.ItemScore;
import com.crayon.scoreboard.util.GDB;
import com.crayon.scoreboard.MainActivity;
import com.crayon.scoreboard.R;
import com.crayon.scoreboard.views.TextClockEx;

import java.util.HashMap;

import com.aphidmobile.flip.FlipViewController;
import com.google.gson.Gson;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment implements View.OnClickListener
        ,CompoundButton.OnCheckedChangeListener
        , FlipTextViewFragment.FlipTextSelectedFragmentListener
        ,FlipViewController.ViewFlipListener{
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    HashMap<Integer,FlipTextViewFragment> map = new HashMap<>();

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

//    public PlaceholderFragment() {
//    }


    ItemGame itemGame =new ItemGame();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fragmentManager = getChildFragmentManager();
//
        map.put(R.id.fragmentFlipSetBlue,FragmentSetScore.newInstance(Color.BLACK));
        map.put(R.id.fragmentFlipSetRed, FragmentSetScore.newInstance(Color.BLACK));
        map.put(R.id.fragmentFlipBlue,FlipTextViewFragment.newInstance(Color.BLUE));
        map.put(R.id.fragmentFlipRed,FlipTextViewFragment.newInstance(Color.RED));

        FragmentTransaction ft = fragmentManager.beginTransaction();

        for(int nId : map.keySet()){
            FlipTextViewFragment flip = (FlipTextViewFragment)map.get(nId);
            flip.setSelectedFragmentListener(this);
            ft.replace(nId, flip);

        }
        ft.commit();

        TextClockEx textClockEx = (TextClockEx)getView().findViewById(R.id.sectionTextTimeCounter);

        ( (ToggleButton)getView().findViewById(R.id.buttonPause)).setOnCheckedChangeListener(this);
        getView().findViewById(R.id.buttonReSet).setOnClickListener(this);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.buttonReSet://세트종료...

                FlipTextViewFragment flipBlue = (FlipTextViewFragment)map.get(R.id.fragmentFlipBlue);
                FlipTextViewFragment flipRed = (FlipTextViewFragment)map.get(R.id.fragmentFlipRed);

                FragmentSetScore fragmentFlipSetBlue = (FragmentSetScore)map.get(R.id.fragmentFlipSetBlue);
                FragmentSetScore fragmentFlipSetRed = (FragmentSetScore)map.get(R.id.fragmentFlipSetRed);
                if(flipBlue.getFlipBoardIndex()>flipRed.getFlipBoardIndex()){
                    fragmentFlipSetBlue.incressCount();
                }else if(flipBlue.getFlipBoardIndex()<flipRed.getFlipBoardIndex()){
                    fragmentFlipSetRed.incressCount();
                }
                flipBlue.setSelectIndex(0);
                flipRed.setSelectIndex(0);
                TextClockEx textClockEx = (TextClockEx)getView().findViewById(R.id.sectionTextTimeCounter);
                textClockEx.setReset();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch(buttonView.getId()){
            case R.id.buttonPause:
                TextClockEx textClockEx = (TextClockEx)getView().findViewById(R.id.sectionTextTimeCounter);
                if(isChecked){
                    textClockEx.setPause();
                }else{
                    textClockEx.setRestart();
                }
                break;
        }

    }

    @Override
    public void OnFlipSelectedScoreAtFragment(FlipTextViewFragment fragment, View view, int position) {
        GDB.log("...fragment id " + fragment.getId());
        GDB.log("...fragment id " + fragment.getFlipBoardIndex());
        GDB.log("...fragment id " + position);
        //스코어 이동에 따른 데이타를 저장한다.
        ItemScore item = new ItemScore();
        item.setScore(1);
        if(fragment.getId()==R.id.fragmentFlipRed){
            item.setTeam(getString(R.string.data_team_name_red));
        }else if(fragment.getId()==R.id.fragmentFlipBlue){
            item.setTeam(getString(R.string.data_team_name_blue));
        }
        itemGame.appendScore(item);
    }

    @Override
    public void onViewFlipped(View view, int position) {
        Gson gson = new Gson();
    }

    public ItemGame OnSaveGameData(){

        if(itemGame==null ||itemGame.getScores().size()<=0) return null;

        Gson gson =new Gson();
        String data = gson.toJson(itemGame);
        GDB.log("data " + data);


//        getActivity().getCacheDir()
//        getActivity().openFileOutput()

        return itemGame;
    }
}
